import 'package:hongsheng/model/SPClassTaskEntity.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';




///
/// @author tsing
/// @time 2018/10/15 下午2:33
/// @email shuqing.li@merculet.io
///
class SPClassTaskToast {
  static OverlayEntry ?_overlayEntry; //toast靠它加到屏幕上
  static bool _showing = false; //toast是否正在showing
  static bool _dismiss = false; //toast是否消息
  static DateTime ?_startedTime; //开启一个新toast的当前时间，用于对比是否已经展示了足够时间
  static SPClassTaskEntity ?_taskData; //开启一个新toast的当前时间，用于对比是否已经展示了足够时间
  static void spFunToast(
      BuildContext context,
      SPClassTaskEntity taskData,
      ) async {
    _startedTime = DateTime.now();
    //获取OverlayState
    OverlayState? overlayState = Overlay.of(context);
    _showing = true;
    _dismiss = false;
    _taskData =taskData;
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(
          builder: (BuildContext context) => Positioned(
            //top值，可以改变这个值来改变toast在屏幕中的位置
            top: MediaQuery.of(context).size.height * 5 / 6,
            child: Container(
                alignment: Alignment.center,
                width: MediaQuery.of(context).size.width,
                child:_showing ?AnimatedOpacity(
                  opacity: !_dismiss ? 1.0 : 0.0, //目标透明度
                  duration: !_dismiss
                      ? Duration(milliseconds: 100)
                      : Duration(seconds: 1),
                  child: spFunBuildToastWidget(),
                ):SizedBox()),
          ));
      overlayState?.insert(_overlayEntry!);
    } else {
      //重新绘制UI，类似setState
      _overlayEntry?.markNeedsBuild();
    }
    Future.delayed(Duration(milliseconds: 4000),(){
      _dismiss=true;
      _overlayEntry?.markNeedsBuild();
    });
    await Future.delayed(Duration(milliseconds: 5000)); //
    //2秒后 到底消失不消失
    if (DateTime.now().difference(_startedTime!).inMilliseconds >= 50000) {//5000
      _showing = false;
      _overlayEntry?.markNeedsBuild();
    }
  }

  //toast绘制
  static spFunBuildToastWidget() {
    return Material(
      color: Colors.transparent,
      child: Column(
        children: <Widget>[
          Container(
            height: 43,
            margin: EdgeInsets.symmetric(horizontal:width(60)),
            padding: EdgeInsets.only(left: width(20),right: width(20)),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(150),
                color: MyColors.grey_33.withOpacity(0.8)
            ),
            child: Row(
              children: <Widget>[
                Image.asset(
                  SPClassImageUtil.spFunGetImagePath(
                      "ic_jifen"),
                  width: width(30),
                ),
                Text('+${_taskData!.spProPoints}',style: TextStyle(fontSize: sp(12),color: Colors.white),),
                // SizedBox(width: width(25),),
                Expanded(
                  child:Text('${_taskData!.spProMsg}',style: TextStyle(fontSize: sp(12),color: Colors.white),overflow: TextOverflow.ellipsis,textAlign: TextAlign.center,),
                ),
                (_taskData!.spProComplete==_taskData!.spProCount)?
                Image.asset(
                  SPClassImageUtil.spFunGetImagePath(
                      "jiantou_right"),
                  width: width(30),
                  color: Colors.white,
                ):RichText(
                    text: TextSpan(
                        text: '${_taskData!.spProComplete}',
                        style: TextStyle(fontSize: sp(12),color: Colors.red),
                        children: [
                          TextSpan(
                              text: '/${_taskData!.spProCount}',
                              style: TextStyle(fontSize: sp(12),color: Colors.white)
                          ),
                        ]
                    )
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}